Nginx静态资源配置

您所在的位置:网站首页 nginx加载接口对的 静态资源路径不对 Nginx静态资源配置

Nginx静态资源配置

2024-07-11 15:22| 来源: 网络整理| 查看: 265

Nginx配置成系统服务

把Nginx应用服务设置成为系统服务,方便对Nginx服务的启动和停止等相关操作,具体实现步骤:

在/usr/lib/systemd/system目录下添加nginx.service,内容如下: [Unit] # Unit表明该服务的描述,类型描述 Description=nginx web service Documentation=http://nginx.org/en/docs/ # 要求在network服务启动后再启动 After=network.target [Service] # 运行模式 simple|forking|oneshot|dbus|notify Type=forking # 存放PID文件的位置 PIDFile=/usr/local/nginx/logs/nginx.pid # ExecStart被执行之前被执行 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf # 服务运行的具体执行命令 ExecStart=/usr/local/nginx/sbin/nginx # 服务重启的执行命令 ExecReload=/usr/local/nginx/sbin/nginx -s reload # 服务停止的执行命令 ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=default.target 添加完成后如果权限有问题需要进行权限设置

chmod 755 /usr/lib/systemd/system/nginx.service

使用系统命令来操作Nginx服务.

启动: systemctl start nginx 停止: systemctl stop nginx 重启: systemctl restart nginx 重新加载配置文件: systemctl reload nginx 查看nginx状态: systemctl status nginx 开机启动: systemctl enable nginx

静态资源配置指令 设置请求资源的目录 root:设置请求的根目录 | 语法 | root path; | | — | — | | 默认值 | root html; | | 位置 | http、server、location |

path为Nginx服务器接收到请求以后查找资源的根目录路径。

alias:用来更改location的URI | 语法 | alias path; | | — | — | | 默认值 | — | | 位置 | location |

path为修改后的根路径。 案例: 在/usr/local/nginx/html目录下创建一个 images目录,并在目录下放入一张图片1.jpg图片 ../html/ ├── 50x.html ├── html │ ├── 50x.html │ └── index.html ├── images │ └── 1.jpg └── index.html

server { listen 8070; server_name 127.0.0.1; location /images { root /home/nginx/html; } } # 访问图片的路径为:http://172.41.100.15:8070/images/1.jpg server { listen 8071; server_name localhost; location /images { #把root改为alias alias /home/nginx/html; } # 正确配置 location /imagess { alias /home/nginx/html/images; } } # 访问http://172.41.100.15:8071/images/1.jpg 报404 # 正确访问图片的路径为:http://172.41.100.15:8071/imagess/1.jpg root与alias的区别

root的处理结果是: root路径+location路径 /usr/local/nginx/html/images/1.jpg alias的处理结果是: 使用alias路径替换location路径 /usr/local/nginx/html/1.jpg 如果location路径是以/结尾,则alias也必须是以/结尾,root没有要求 alias是一个目录别名的定义,root则是最上层目录的含义。 例如:

location /imagess/ { alias /home/nginx/html/images; } # 访问就会出问题,查看错误日志还是路径不对,所以需要把alias后面加上 / 2021/08/16 16:14:14 [error] 27736#0: *57 open() "/home/nginx/html/images1.jpg" failed (2: No such file or directory), client: 172.41.100.13, server: localhost, request: "GET /images1/1.jpg HTTP/1.1", host: "172.41.100.15:8071" index指令

index:设置网站的默认首页

语法index file …;默认值index index.html;位置http、server、location

index后面可以跟多个设置,如果访问的时候没有指定具体访问的资源,则会依次进行查找,找到第一个为止。 案例:

location / { root /home/nginx/html; index index.html index.htm; }

访问该location的时候,可以通过 http://ip:port/,地址后面如果不添加任何内容,则默认依次访问index.html和index.htm,找到第一个来进行返回

error_page指令

error_page:设置网站的错误页面

语法error_page code … [=[response]] uri;默认值—位置http、server、location…

当出现对应的响应code后,如何来处理。 案例:

# 指定具体跳转的地址 server { error_page 404 http://www.baidu.com; } # 指定重定向地址 server{ error_page 404 /50x.html; error_page 500 502 503 504 /50x.html; location =/50x.html{ root html; } } # location的@ 完成错误信息展示 server{ error_page 404 @jump_to_error; location @jump_to_error { default_type text/plain; return 404 'Not Found Page...'; } } # =[response]的作用是用来将相应代码更改为另外一个 server{ error_page 404 =200 /50x.html; location =/50x.html{ root html; } } #当返回404找不到对应的资源的时候,在浏览器上可以看到,最终返回的状态码是200 #编写error_page后面的内容,404后面需要加空格,200前面不能加空格 静态资源优化配置

Nginx对静态资源从三个属性配置进行优化:sendfile on、tcp_nopush on、tcp_nodeplay on

sendfile:用来开启高效的文件传输模式。 | 语法 | sendfile on |off; | | — | — | | 默认值 | sendfile off; | | 位置 | http、server、location… |

请求静态资源的过程:

客户端通过网络接口向服务端发送请求.操作系统将这些客户端的请求传递给服务器端应用程序.服务器端应用程序会处理这些请求.请求处理完成以后,操作系统还需要将处理得到的结果通过网络适配器传递回去。

在这里插入图片描述

例如:

server { listen 80; server_name localhost; location / { root html; index index.html; } } # 在html目录下有一个welcome.html页面,访问地址 http://172.41.100.15/welcome.html

未使用sendfile流程: 在这里插入图片描述

使用sendfile流程: 在这里插入图片描述

tcp_nopush:该指令必须在sendfile打开的状态下才会生效,主要是用来提升网络包的传输’效率’,相当于开启缓冲区 | 语法 | tcp_nopush on|off; | | — | — | | 默认值 | tcp_nopush off; | | 位置 | http、server、location |

tcp_nodelay:该指令必须在keep-alive连接开启的情况下才生效,来提高网络包传输的’实时性’,即有数据立刻发送 | 语法 | tcp_nodelay on|off; | | — | — | | 默认值 | tcp_nodelay on; | | 位置 | http、server、location |

sendfile可以开启高效的文件传输模式,tcp_nopush开启可以确保在发送到客户端之前数据缓冲区已经充分“填满”, 这大大减少了网络开销,并加快了文件发送的速度。 然后,当它到达最后一个可能因为没有“填满”而暂停的数据包时,Nginx会忽略tcp_nopush参数, 然后,tcp_nodelay强制套接字发送数据。由此可知,tcp_nopush可以与tcp_nodelay一起设置,它比单独配置tcp_nodelay具有更强的性能。

**



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3